package com.qili.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qili.entity.base.BaseDict;
import com.qili.entity.supplier.ProjectCollect;
import com.qili.service.base.BaseDictService;
import com.qili.service.supervisor.SupervisorFileTempService;
import com.qili.service.supplier.ProjectCollectService;
import com.qili.service.supplier.RecommendReasonService;
import com.qili.util.CommonUtil;
import com.qili.util.DateUtil;
import com.qili.util.JsonUtil;
import com.qili.util.ReType;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangxiaopeng
 * @date 2021-04-06
 * @description 供需系统规则中心
 */
@Controller
@RequestMapping("/supplier/ruleCentre")
public class RuleCentreController {

    @Autowired
    ProjectCollectService pcService;
    @Autowired
    RecommendReasonService reService;
    @Autowired
    BaseDictService baseDicService;
    @Autowired
    SupervisorFileTempService supervisorFileTempService;
    /**
     * @param request
     * @Author:zhangxiaopeng
     * @Description: 规则中心页面
     * @Date:13:38 2021/4/6
     */
    @GetMapping(value = "rule")
    @RequiresPermissions("supplier:gzcenter")
    public String index(HttpServletRequest request) {
        return "projectManager/ruleCentre";
    }
    /**
     * @param request
     * @Author:zhangxiaopeng
     * @Description: 我的收藏页面
     * @Date:13:38 2021/4/6
     */
    @GetMapping(value = "collect")
    @RequiresPermissions("supplier:mysc")
    public String collect(HttpServletRequest request) {
        return "projectManager/projectCollect";
    }

    /**
     * 查询我的收藏
     * @param model
     * @param projectCollect
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("selectCollect")
    @ResponseBody
    public ReType selectCollect(Model model, ProjectCollect projectCollect, int page, int limit){
         String userId= CommonUtil.getUserId();
         projectCollect.setCreateBy(userId);
        Page<ProjectCollect> tPage = PageHelper.startPage(page, limit);
        List<ProjectCollect> list=pcService.selectCollect(projectCollect);
        return new ReType(tPage.getTotal(),list);
    }

    /**
     * 取消收藏
     * @param id
     * @return
     */
    @RequestMapping("deleteCollect")
    @ResponseBody
    public JsonUtil deleteCollect(String id,String projectId){
        JsonUtil json=new JsonUtil();
        Map map=new HashMap();
        ProjectCollect pc=new ProjectCollect();
        if(StringUtils.isNotBlank(id)){
            map.put("id",id);
            pc.setId(id);
        }else if(StringUtils.isNotBlank(projectId)){
            map.put("projectId",projectId);
            pc.setProjectId(projectId);
        }
        List<ProjectCollect> list=pcService.selectCollect(pc);
        if(list==null||list.size()==0){
            json.setMsg("已取消过收藏");
            json.setFlag(false);
            return json;
        }
        if(pcService.deleteCollect(map)>0){
            json.setMsg("取消成功");
            json.setFlag(true);
            return json;
        }
        json.setMsg("取消失败");
        json.setFlag(false);
        return json;
    }

    /**
     * 收藏
     * @param id
     * @return
     */
    @RequestMapping("insertCollect")
    @ResponseBody
    public JsonUtil insertCollect(String id){
        JsonUtil json=new JsonUtil();
        String userId= CommonUtil.getUserId();
        ProjectCollect co=new ProjectCollect();
        co.setProjectId(id);
        co.setCreateBy(userId);
        List<ProjectCollect> list=pcService.selectCollect(co);
        if(list!=null&&list.size()>0){
            json.setMsg("已经收藏过！");
            json.setFlag(false);
            return json;
        }
        co.setId(IdUtil.simpleUUID());
        co.setCreateDate(DateUtil.getCurrentDateTimeString());
        if(pcService.insertSelective(co)>0){
            json.setMsg("收藏成功");
            json.setFlag(true);
            return json;
        }
        json.setMsg("收藏失败");
        json.setFlag(false);
        return json;
    }

    //查询字典中发布指南
    @RequestMapping("publishGuide")
    @ResponseBody
    public String publishGuide(BaseDict b){
//        b.setBadtCode("042");
//        b.setBadtTypecode("LC001");
        BaseDict baseDict = baseDicService.selectOne(b);
        return baseDict.getBadtLevel9();
    }

    //上传合同关闭删除临时文件
    @RequestMapping("deleteFile")
    public void deleteFile(String id){
        supervisorFileTempService.deleteByPrimaryKey(id);
    }
}
